(function ($) {
    $.fn.hmall = function (options) {
        var settings = {};
        var p = 1, cbd = 1;
        if (options) {
            $.extend(settings, options);
        }
        var addHtml = function (html, reset) {
            addMoreEvent(html.data.ap);
            if (!reset) {
                $("#hmalls").html(html.data.malls);
            } else {
                $("#hmalls").append(html.data.malls);
            }
            imgLazy();
        }
        var getHtml = function (i, p) {
            if (p == 1) {
                $("#hmalls").html("");
            }
            $url = m.app + "/bus/mall/ad/" + i + "/" + p;
            $.get($url, function (result) {
                if (result.status == 1) {
                    addHtml(result, p);
                }
            }, "json");
        }
        var addMoreEvent = function (allpage) {
            if (allpage > p) {
                $("div.moremall>a.more").unbind('click');// BUG防止重复绑定
                $("div.moremall>a.more").click(function () {
                    p++;
                    getHtml(cbd, p);
                    if (p >= allpage) {
                        $("div.moremall").hide();
                    }
                });
                $("div.moremall").show();
            } else {
                $("div.moremall").hide();
            }
        }
        $("div.tiny>a").click(function () {
            var self = this;
            // Remove on
            $('div.tiny>a').removeClass('on');
            // This add on class
            $(self).addClass('on');
            // Get html
            cbd = $(self).data('bid');
            p = 1;
            getHtml(cbd, p);
        });
        // Default load first
        getHtml(cbd, p);
        //Select li
        $("div.tiny>a:first").addClass('on');
    }
})(jQuery);